From f1910544cf6df50bcb175b66b604f5903f6ae3fa Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Sat, 27 Jul 2024 13:34:00 +0700
Subject: <iman> update category management for lob

---
 src/pages/shop/lob/[slug].jsx | 71 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 54 insertions(+), 17 deletions(-)

(limited to 'src/pages/shop/lob/[slug].jsx')

diff --git a/src/pages/shop/lob/[slug].jsx b/src/pages/shop/lob/[slug].jsx
index be8db639..d02ec8ac 100644
--- a/src/pages/shop/lob/[slug].jsx
+++ b/src/pages/shop/lob/[slug].jsx
@@ -3,9 +3,10 @@ import dynamic from 'next/dynamic';
 import { useRouter } from 'next/router';
 import Seo from '@/core/components/Seo';
 import { getIdFromSlug, getNameFromSlug } from '@/core/utils/slug';
-import Breadcrumb from '@/lib/category/components/Breadcrumb';
+import Breadcrumb from '../../../lib/lob/components/Breadcrumb';
 import { useEffect, useState } from 'react';
 import odooApi from '@/core/api/odooApi';
+import { div } from 'lodash-contrib';
 
 const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'));
 const ProductSearch = dynamic(() => import('@/lib/product/components/ProductSearch'));
@@ -16,9 +17,20 @@ export default function CategoryDetail() {
   const { slug = '', page = 1 } = router.query;
   const [dataLob, setDataLob] = useState([]);
   const [finalQuery, setFinalQuery] = useState({});
+  const [dataCategoriesProduct, setDataCategoriesProduct] = useState([])
+  const [data, setData] = useState([])
+  const dataIdCategories = []
   
   const categoryName = getNameFromSlug(slug);
   const lobId = getIdFromSlug(slug);
+  const q = router?.query.q || null;
+  const query = {
+    fq: `id:${lobId}`,
+    page,
+  };
+  if (q) {
+    query.q = q;
+  }
 
   useEffect(() => {
     const fetchCategoryData = async () => {
@@ -33,22 +45,40 @@ export default function CategoryDetail() {
     fetchCategoryData();
   }, [lobId]);
 
-  console.log("dataLob",dataLob)
-  
+  const collectIdsCategory = (category) => {
+    const ids = [];
+    function recurse(cat) {
+      if (cat && cat.id) {
+        ids.push(cat.id);
+      }
+      if (Array.isArray(cat.children)) {
+        cat.children.forEach(recurse);
+      }
+    }
+    recurse(category);
+    return ids;
+  };
   useEffect(() => {
-      const collectIds = (categories) => {
-          return categories?.categoryIds?.map(data => data.id) || [];
-        };
-        
-        const ids = collectIds(dataLob[0]);
-        const newQuery = {
-            fq: `category_id_ids:(${ids.join(' OR ')})`,
-            page: 1,
-            brand: router.query.brand || '',
-        };
-        setFinalQuery(newQuery);
-        console.log("finalQuery",finalQuery)
-  }, [dataLob, router.query.brand]);
+    const fetchCategoryData = async () => {
+      if (dataLob[0]?.categoryIds) {
+        for (const category of dataLob[0].categoryIds) {
+          const getCategoriesId = await odooApi('GET', `/api/v1/category/numFound?parent_id=${category.id}`);
+          if (getCategoriesId) {
+            setDataCategoriesProduct(getCategoriesId);
+            const ids = collectIdsCategory(getCategoriesId);
+            dataIdCategories.push(ids)
+          }
+        }
+        setData(dataIdCategories)
+      }
+    };
+    fetchCategoryData();
+    const mergedArray = data.flat();
+    const newQuery = {
+      fq: `category_id_ids:(${mergedArray.join(' OR ')})`,
+    };
+    setFinalQuery(newQuery);
+  }, [dataLob, ]);
 
   return (
     <BasicLayout>
@@ -64,9 +94,16 @@ export default function CategoryDetail() {
       />
 
       <Breadcrumb categoryId={getIdFromSlug(slug)} />
+      <div className='grid grid-cols-3'>
+        {dataLob[0]?.categoryIds && (
+          <div>
+            <image></image>
+          </div>
+        )}
+      </div>
 
       {!_.isEmpty(router.query) && (
-        <ProductSearch query={finalQuery} categories={getIdFromSlug(slug)} prefixUrl={`/shop/lob/${slug}`} />
+        <ProductSearch query={query} categories={getIdFromSlug(slug)} prefixUrl={`/shop/lob/${slug}`} />
       )}
     </BasicLayout>
   );
-- 
cgit v1.2.3

